package top.wangjiu.reactive.rpc.loadbalance;

import top.wangjiu.reactive.rpc.client.RpcClient;
import top.wangjiu.reactive.rpc.client.RpcClientContainer;
import top.wangjiu.reactive.rpc.model.RpcRequest;

import java.net.InetSocketAddress;
import java.util.*;


/**
 * 负载均衡器
 */
public class RandomLoadBalance implements LoadBalance{

    private final Random random;

    public RandomLoadBalance(int seed){
        this.random = new Random(seed);
    }

    public RandomLoadBalance(){
        this.random = new Random(0);
    }

    @Override
    public RpcClient getClient(RpcRequest request, List<InetSocketAddress> addressList,RpcClientContainer clientContainer) {
        int i = random.nextInt(addressList.size());
        return clientContainer.getClient(addressList.get(i));
    }
}
